package com.example.otpdemo2;

//import com.demo.twostep.service.GoogleAuthenticatorService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @date: 2023/10/25 12:10
 * @author: hp
 * @description: TODO
 */

@Slf4j
@RestController
public class ActionController {

    @Autowired
    private GoogleAuthenticatorService googleAuthenticatorService;

    /**
     * 二次验证，生成二维码
     */
    @RequestMapping("/qrcode")
    public void qrcode(String username, HttpServletResponse response) {
        try (ServletOutputStream stream = response.getOutputStream()) {
            googleAuthenticatorService.genQRImage(username, stream);
        } catch (IOException e) {
            log.error("发生错误", e);
        }
    }

    /**
     * 二次验证，输入eagle2fa APP上的6位数字
     */
    @RequestMapping("/verify")
    public String verify(String username, int code) {

        boolean validCode = googleAuthenticatorService.validCode(username, code);

        if (validCode) {

            return "index";
        }

        return "error";
    }
}
